/**
 * @file utils.h
 * Utility function and macro definitions for the sources.
 */

/* Created by Laszlo Ashin <kodest@gmail.com> in 2oo8. */

#ifndef _UTILS_H
#define _UTILS_H	1

#include <string.h>
#include <sys/types.h>


/**
 * Configuration file parsing helper function for reading logical values.
 * @param s Input string to convert.
 * @param bo Output logical value.
 * @return Zero on success.
 */
int cfgstr2bool(const char *s, int *bo);

/**
 * Configuration file parsing helper function for reading integer values.
 * @param s Input string to convert.
 * @param i Output integer value.
 * @return Zero on success.
 */
int cfgstr2int(const char *s, int *i);

/**
 * Configuration file parsing helper function for reading floating-point
 * values.
 * @param s Input string to convert.
 * @param d Output floating-point value.
 * @return Zero on success.
 */
int cfgstr2double(const char *s, double *d);

/**
 * Collects data from input files containing name-value pairs.
 * @param fname Name of the input file to read data from.
 * @param sep Delimiter string between names and values.
 * @param num Number of items to search for.
 * @param idlist Array of strings representing the names to search for.
 * @param valuelist The destination of the values.
 * @return Zero on success.
 */
int collect_data_from_file(const char *fname, const char *sep, int num,
	const char *idlist[], char *valuelist[]);

/**
 * Encodes a string using the well-known base64 algorithm.
 * @param s String to encode.
 * @param len Number of bytes to encode.
 * @return Pointer to the encoded data. Caller frees.
 */
/*@null@*/ /*@out@*/ char *base64_encode(const char *s, size_t len);

/**
 * Convert a string to floating-point value with making use of SI prefices if
 * they occur after the value in the input string.
 * @param s String we want to convert.
 * @param f Result as a floating-point number.
 * @param endpos Pointer to the fragment of the specified string where the
 *        parse finished.
 * @return Zero on success.
 */
int parse_prefixed_string(const char *s, double *f, char **endpos);

/**
 * Read a password from the user.
 * @param buf Destination buffer for the read password.
 * @param bufsize Size of the specified buffer.
 * @return Number of characters the user entered.
 */
ssize_t get_pass(char *buf, size_t bufsize);

/**
 * Returns the current time in seconds as a floating point value.
 * @return The current time.
 */
double gettimeofdayd(void);

/**
 * Converts the given double into a timeval struct.
 * @param d Double value containst time in seconds.
 * @param tv Destination timeval structure.
 */
void double2timeval(double d, /*@out@*/ struct timeval *tv);

/**
 * Sets the separator among elements.
 * @param s A string to use as separator;
 */
int set_separator(const char *s);

/**
 * Shows an element in the output. Takes care of the separator.
 * @param fmt Format string
 * @return Number of characters the shown element takes.
 */
int show(const char *fmt, ...);

/**
 * Signs that a new line will come of the output.
 */
void show_new_line(void);

/**
 * Shows an error message on the standard error output.
 * Closes the line.
 */
void show_error(const char *fmt, ...);

/**
 * Memory allocation helper function contains NULL return value check.
 */
/*@only@*/ /*@out@*/ /*@null@*/ void *xalloc(size_t size) /*@*/
    /*@ensures maxSet(result) == (size - 1); @*/;

/** Checks if the first string starts with the second one. */
#define strstartswith(x, y) (strncmp(x, y, strlen(y)) == 0)

/** 
 * Allocates memory for a new object instance and fills the allocated memory
 * area with zeroes. Simplifies memory allocation.
 */
#define NEW(c) ((c *)xalloc(sizeof(c)))

#endif /* _UTILS_H */
